package com.hackaton.discounts;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class LatestActivity extends Activity {
	private ListView listView;
	private ArrayList<Discount> list; 
	ListAdapter adapter;
	DatabaseManager db;
    protected void onCreate(Bundle savedInstanceState){
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.latest);
    	listView=(ListView)findViewById(R.id.listLatest);
    	listView.setOnItemClickListener(new OnItemClickListener(){

    		
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays();
				ArrayList<Object> row = data.get((int) id);
				Intent i=new Intent(getBaseContext(), DetailedView.class);
				i.putExtra("title", list.get(position).title);
				i.putExtra("text", list.get(position).text);
				i.putExtra("imgUri", list.get(position).imgUri);
				i.putExtra("percentage", row.get(5).toString());
				i.putExtra("price", row.get(6).toString());
				startActivity(i);
			}
    		
    	});
    	list=getData();
    	adapter=new ListAdapter(this, list);
    	listView.setAdapter(adapter);
    	
    }
    public ArrayList<Discount> getData(){
    	ArrayList todoItems = new ArrayList();
    	
        db = new DatabaseManager(this);
    	ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays();
    	for (int position=0; position < data.size(); position++)
    	{
    		ArrayList<Object> row = data.get(position);
    		Discount featurData = new Discount();
    		featurData.title = row.get(2).toString();
    		featurData.text = row.get(3).toString();
    		featurData.imgUri = row.get(4).toString() + ".jpg";
    		todoItems.add(featurData);
    	}
    	return todoItems;
    }
}